/**
 * 
 */
package com.zxc.redis.RMap;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import com.zxc.redis.AnyObject;

/**
 * @author Administrator
 *
 */
public class RMapDemo {

	/**
	 * @param args
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		Config config = new Config();
		config
			//.setCodec(new FstCodec())
			.useSingleServer() //单例模式
			.setAddress("192.168.1.122:6379")//连接地址
			.setDatabase(1) // 数据库索引位置
			.setPassword("shijie99test") // 密码
			.setClientName("channelbatch") // 客户端名称
			;
		RedissonClient redisson = Redisson.create(config);

		RMap<String, AnyObject> map = redisson.getMap("anyMap");
		
		AnyObject prevObject = map.put("123", new AnyObject(123));
		System.out.println("key : 123,prevObject : "+prevObject);
		System.out.println("key : 123,currentObject : "+map.get("123"));
		
		AnyObject currentObject = map.putIfAbsent("323", new AnyObject(323));
		System.out.println("key : 323,value : "+currentObject);
		
//		AnyObject obj = map.remove("123");
//		System.out.println("remove key : 123,value : "+obj);
		
		boolean res = map.fastPut("321", new AnyObject(321));
		System.out.println("fastPut key 321 result :"+res);
		
//		map.fastRemove("321");
		
		Future<AnyObject> putAsyncFuture = map.putAsync("321",new AnyObject(1));
		System.out.println("putAsync key 321 after result : " + putAsyncFuture.get());
		
		Future<Boolean> fastPutAsyncFuture = map.fastPutAsync("321",new AnyObject(2));
		System.out.println("fastPutAsync key 321 after result : " + fastPutAsyncFuture.get());
		
//		map.fastRemoveAsync("321");
		
		// 删除key
//		map.clear();
		
		// Map eviction
		// 现在Redis没有过期清空Map中的某个entry的功能，只能是清空Map所有的entry。Redission提供了这种功能。
		RMapCache<String, AnyObject> cache = redisson.getMapCache("anyMapCache");
		
		// ttl = 10 seconds, 
		cache.put("key1", new AnyObject(1), 5, TimeUnit.SECONDS);
		System.out.println("RMapCache key1 's current value :"+cache.get("key1"));
		
		// ttl = 10 seconds, maxIdleTime = 10 seconds
		cache.put("key1", new AnyObject(2), 10, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
		System.out.println("RMapCache key1 's current value :"+cache.get("key1"));
		
		// ttl = 3 seconds
		cache.putIfAbsent("key2", new AnyObject(3), 3, TimeUnit.SECONDS);
		System.out.println("RMapCache key2 's current value :"+cache.get("key2"));
		
		// ttl = 10 seconds, maxIdleTime = 10 seconds
		cache.putIfAbsent("key2", new AnyObject(4), 10, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
		System.out.println("RMapCache key2 's current value :"+cache.get("key2"));
		
		
		System.out.println("**********************************************************");
		System.out.println("sleep 20 seconds");
		Thread.sleep(20*1000);
//		System.out.println("RMapCache key1 de current value :"+cache.get("key1"));
//		System.out.println("RMapCache key2 de current value :"+cache.get("key2"));
		
		redisson.shutdown();
	}

}
